﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class LoginLimit_tbService
    {


        /// <summary>
        /// 根据IP获取数据
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static LoginLimit_tb GetLoginLimit(string ip)
        {
            using (MyETLockContext DbContext = new MyETLockContext())
            {
                return DbContext.LoginLimit_tb.FirstOrDefault(x => x.IP == ip);
            }
        }

        /// <summary>
        /// 初始化和正常登陆后恢复初始数据
        /// </summary>
        /// <param name="maxCount"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static LoginLimit_tb InitializeCount(string ip,int userId)
        {
            using (MyETLockContext DbContext = new MyETLockContext())
            {
                LoginLimit_tb loginLimit = DbContext.LoginLimit_tb.FirstOrDefault(x => x.IP == ip);
                if (loginLimit != null)
                {
                    if (loginLimit.ErrCount > 0)
                    {
                        loginLimit.ErrCount = 0;
                        loginLimit.CUserId = userId;
                    }
                    loginLimit.UnlockTime = DateTime.Now.AddMinutes(-1);
                }
                else
                {
                    LoginLimit_tb newLoginLimit_tb = new LoginLimit_tb();
                    newLoginLimit_tb.ErrCount = 0;
                    newLoginLimit_tb.IP = ip;
                    newLoginLimit_tb.CUserId = userId;
                    newLoginLimit_tb.UnlockTime = DateTime.Now.AddMinutes(-1);
                    DbContext.LoginLimit_tb.Add(newLoginLimit_tb);
                    loginLimit = newLoginLimit_tb;
                }

                 DbContext.SaveChanges();
                return loginLimit;

            }
        }

        /// <summary>
        /// 更新错误次数与初始化 并返回最新的记录
        /// </summary>
        /// <param name="maxCount"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static LoginLimit_tb UpdateCount(int maxCount, int minutes, string ip)
        {
            using (MyETLockContext DbContext = new MyETLockContext())
            {
                int count = 0;
                LoginLimit_tb loginLimit_tb = DbContext.LoginLimit_tb.FirstOrDefault(x => x.IP == ip);
                if (loginLimit_tb != null)
                {

                    if (loginLimit_tb.ErrCount >= maxCount - 1)
                    {
                        //是当天内
                        int days = DateTime.Now.Day - loginLimit_tb.UnlockTime.Day;
                        if (loginLimit_tb.ErrCount > 0 && days >= 1)
                        {
                            loginLimit_tb.ErrCount = 1;
                            loginLimit_tb.UnlockTime = DateTime.Now.AddMinutes(-1);
                        }
                        else if (loginLimit_tb.ErrCount > maxCount)
                        {
                            loginLimit_tb.ErrCount = maxCount;
                            loginLimit_tb.UnlockTime = DateTime.Now.AddMinutes(minutes);
                        }
                        else
                        {
                            loginLimit_tb.ErrCount++;
                            loginLimit_tb.UnlockTime = DateTime.Now.AddMinutes(minutes);
                        }

                    }
                    else
                    {
                        loginLimit_tb.ErrCount++;
                    }
                    count = loginLimit_tb.ErrCount;
                }
                else
                {
                    LoginLimit_tb newLoginLimit_tb = new LoginLimit_tb();
                    newLoginLimit_tb.ErrCount = 1;
                    newLoginLimit_tb.IP = ip;
                    newLoginLimit_tb.UnlockTime = DateTime.Now.AddMinutes(-1);
                    DbContext.LoginLimit_tb.Add(newLoginLimit_tb);
                    loginLimit_tb = newLoginLimit_tb;
                }

                DbContext.SaveChanges();
                return loginLimit_tb ;
            }
        }

    }
}
